package com.huaiwei.usmManager.modules.usm.controller;

import com.huaiwei.usmManager.common.utils.R;
import com.huaiwei.usmManager.modules.usm.entity.CabinetEntity;
import com.huaiwei.usmManager.modules.usm.service.CabinetService;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import com.huaiwei.usmManager.modules.usm.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/cabinet")
@Api(tags = "展示柜管理")
public class CabinetController {

    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;

    @GetMapping("/list")
    @ApiOperation("展示柜分页查询")
    public R list(@RequestParam Map<String,Object> params){
        return R.ok().put("page",cabinetService.queryPage(params));
    }

    @PostMapping("/update")
    @ApiOperation("添加或修改展示柜信息")
    public R update(@RequestBody CabinetEntity cabinet){
        cabinetService.insertOrUpdate(cabinet);
        return R.ok();
    }

    @GetMapping("/info/{cabinetId}")
    @ApiOperation("根据id获取展示柜信息")
    public R info(@PathVariable("cabinetId") Integer cabinetId){
        //return R.ok().put("list",cabinetService.getById(cabinetId));
        return R.ok().put("list",cabinetService.getInfoByCabinetId(cabinetId));
    }

    @PostMapping("/status")
    @ApiOperation("修改展示柜状态")
    public R status(@RequestBody CabinetEntity cabinet){
        cabinetService.saveOrUpdate(cabinet);
        return R.ok();
    }

    @DeleteMapping("/delete/{cabinetId}")
    @ApiOperation("删除展示柜")
    public R delete(@PathVariable("cabinetId") Integer cabinetId){
        /*如果findGoodsInCabByCabId方法的结果为真，那么说明展示柜上没有商品，那就可以删除展示柜*/
        if(cabinetService.findGoodsInCabByCabId(cabinetId)){
            cabinetService.removeById(cabinetId);
            return R.ok();
        }else{
            /*反之，展示柜上有商品，就不可以删除*/
            return R.error("货架上还有商品,不可以删除!");
        }
    }

    @GetMapping("/goods/list/{cabinetId}")
    @ApiOperation("根据cabinetId查出包含的商品")
    public R goodsList(@PathVariable("cabinetId") Integer cabinetId,@RequestParam Map<String,Object> params){
        return R.ok().put("page",cabinetService.getGoodsByCabinetId(cabinetId,params));
    }

    @GetMapping("/shop/list/{cabinetId}")
    @ApiOperation("根据cabinetId查出所属的商铺")
    public R shopList(@PathVariable("cabinetId") Integer cabinetId,@RequestParam Map<String,Object> params){
        return R.ok().put("page",cabinetService.getShopByCabinetId(cabinetId,params));
    }

    /*获取所有的商铺信息*/
    @GetMapping("/shoplist")
    @ApiOperation("查出所有商铺")
    public R shopIdList(){
        return R.ok().put("list",shopService.getAllShop());
    }

}
